package com.app.micaihu.view.home.questions.activity

import android.content.Intent
import android.view.View
import com.app.base.BaseMvpActivity
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.bean.infor.UploadFileResult
import com.app.micaihu.configure.Config
import com.app.micaihu.utils.ImageLoadUtils
import com.app.micaihu.view.home.questions.contract.QuestionsContract
import com.app.micaihu.view.home.questions.presenter.QuestionsPresenter
import com.app.micaihu.view.main.view.GlideEngine
import com.app.micaihu.view.main.view.keybord.Control
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.huantansheng.easyphotos.EasyPhotos
import com.huantansheng.easyphotos.models.album.entity.Photo
import com.huantansheng.easyphotos.setting.Setting
import kotlinx.android.synthetic.main.question_add_activity.*
import java.io.File
import java.util.*


/**
 * 问答 提问 暂未使用
 */
class AddQuestionActivity : BaseMvpActivity<QuestionsContract.View, QuestionsPresenter>(), QuestionsContract.View {
    override fun getPresenter() = QuestionsPresenter()

    override fun getLayoutId() = R.layout.question_add_activity

    private val MAX_CHAR = 2000

    private var imagePath = ""

    private var mControl: Control? = null


    override fun setData() {
        etContent.maxByteLength = MAX_CHAR
        biaoqingview.configView(etContent, llMenu, 5, R.drawable.expression_yuandian, thisActivity)
        mControl = Control(llRoot, this)
        mControl?.initContentLayout()
        etTitle.requestFocus()
    }

    override fun addListeners() {
        ivAddPic.setOnClickListener {
            openPhotos()
        }
        menu1.setOnClickListener {
            openPhotos()
        }
        menu2.setOnClickListener {
            if (mControl?.isEmojiLayoutShow!!) {
                if (mControl?.isSoftKeyboardShow!!) {
                    mControl?.onHideClick()
                } else {
                    KeyboardUtils.showSoftInput()
                }
            } else {
                mControl?.onShowClick()
            }
        }
        ivDel.setOnClickListener {
            imagePath = ""
            ivDel.visibility = View.GONE
            ImageLoadUtils.loadImage(R.drawable.posted_ic_pic_more, ivAddPic)
        }
        tvSend.setOnClickListener {
            if (etTitle.text.toString().isEmpty()) {
                ToastUtils.showShort("请输入问题")
                return@setOnClickListener
            }
            if (imagePath.isNotEmpty()) {
                mPresenter?.upFile(File(imagePath))
            }
        }
        etTitle.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus)
                menu2.visibility = View.GONE
        }
        etContent.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus)
                menu2.visibility = View.VISIBLE
        }
    }

    private fun openPhotos() {
        KeyboardUtils.hideSoftInput(this)
        EasyPhotos.createAlbum(this, true, GlideEngine.getInstance())
                .setFileProviderAuthority(Config.fileProvider)
                .setCount(1)
                .setPuzzleMenu(false)
                .setCleanMenu(false)
                .setCameraLocation(Setting.LIST_FIRST)
                .start(Config.ResultCode.resultCodePhoto)
    }


    override fun handleUploadFile(result: BaseBean<UploadFileResult?>?) {
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                result.data?.let {
                    LogUtils.e(it.url)
                }
            }
            ToastUtils.showLong(result.msg)
        }
    }

    override fun onBackPressed() {
        if (mControl?.onBackPressed()!!) {
            super.onBackPressed()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let {
            if (requestCode == Config.ResultCode.resultCodePhoto) {
                //返回对象集合：如果你需要了解图片的宽、高、大小、用户是否选中原图选项等信息，可以用这个
                val resultPhotos: ArrayList<Photo> = it.getParcelableArrayListExtra(EasyPhotos.RESULT_PHOTOS)
                if (resultPhotos.isNotEmpty()) {
                    imagePath = resultPhotos[0].path
                    ImageLoadUtils.loadImage(imagePath, ivAddPic)
                    ivDel.visibility = View.VISIBLE
                }
            }
        }
    }

}